﻿#region

using System;
using FluentRibbon;
using FluentRibbon.Definitions;
using FluentRibbon.Definitions.Controls;
using FluentRibbon.Libraries;
using SpWiki.Lib.Repository;
using WikiPlex;
using R = SpWiki.Layouts.SpWiki.Forms.Ribbon;

#endregion

namespace SpWiki.Layouts.SpWiki.Forms
{
    public partial class Edit : RibbonLayoutsPage
    {
        enum Mode { None, NewFile, ExistingFile }

        #region Properties

        /// <summary>
        /// Standart edit mode is "existingFile"
        /// </summary>
        private Mode _fileMode = Mode.None;

        /// <summary>
        /// The wiki file.
        /// </summary>
        private SpWikiFile _file;

        /// <summary>
        /// The wiki file item
        /// </summary>
        private int _itemd = -1;

        /// <summary>
        /// The wiki library id.
        /// </summary>
        private Guid _listId;

        /// <summary>
        /// Gets the wiki library id.
        /// </summary>
        private Guid ListId
        {
            get
            {
                if (_listId != Guid.Empty) return _listId;
                _listId = new Guid(Request.QueryString["SpWikiList"]);
                return _listId;
            }
        }

        /// <summary>
        /// Gets the file file mode (new or existing).
        /// </summary>
        private Mode FileFileMode
        {
            get
            {
                if (_fileMode != Mode.None) return _fileMode;
                var param = Request.QueryString["mode"].ToLower();
                _fileMode = param == "new" ? Mode.NewFile : Mode.ExistingFile;
                return _fileMode;
            }
        }

        /// <summary>
        /// Gets the wiki file item id.
        /// </summary>
        private int ItemId
        {
            get
            {
                if (_itemd != -1) return _itemd;
                _itemd = int.Parse(Request.QueryString["SpWikiID"]);
                return _itemd;
            }
        }

        /// <summary>
        /// Gets the wiki file.
        /// </summary>
        private SpWikiFile File
        {
            get { return _file ?? (_file = new SpWikiFile(ListId, ItemId)); }
        }

        #endregion  Properties

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Provide ribbon tab definition.
        /// </summary>
        /// <returns>
        /// If you return null here, tab will not be shown.
        /// Overwise, the ribbon tab is created and activated when the page is displayed.
        /// </returns>
        public override TabDefinition GetTabDefinition()
        {
           return
              new TabDefinition
                 {
                    Id = R.RibbonBarId,
                    Title = R.RibbonBarTitle,
                    Groups =
                       new[]
                          {
                             new GroupDefinition
                                {
                                   Id = R.GroupId,
                                   Title = R.GroupTitle,
                                   Template = GroupTemplateLibrary.SimpleTemplate,
                                   Controls =
                                      new ControlDefinition[]
                                         {
                                            new ButtonDefinition
                                               {
                                                  Id = R.ButtonViewId,
                                                  Title = R.ButtonViewTitle,
                                                  CommandJavaScript = "NavigateToUrl('" + File.ViewUrl + "');",
                                                  Image = ImageLibrary.GetStandardImage(2, 14)
                                               },
                                         }
                                }
                          }
                 };
        }

       /// <summary>
        /// Represents the method that handles the PreRender event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            TbContent.Text = File.RawContent;

            //SpWikiRibbon.AddRibbon(Page, File);
        }

        /// <summary>
        /// Submits the edit.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void SubmitEdit(object sender, EventArgs e)
        {
            File.RawContent = TbContent.Text;
            RedirectToViewPage();
        }

        /// <summary>
        /// Previews the edit.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void PreviewEdit(object sender, EventArgs e)
        {
            base.OnPreRender(e);
            var engine = new WikiEngine();
            var output = engine.Render(TbContent.Text);
            LitPreview.Text = output;
        }

        /// <summary>
        /// Cancels the edit.f
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CancelEdit(object sender, EventArgs e)
        {
            RedirectToViewPage();
        }

        /// <summary>
        /// Redirects to view page.
        /// </summary>
        private void RedirectToViewPage()
        {
            Response.Redirect(File.ViewUrl);
        }
    }
}